package com.x_memteor.base_master.utils;

/**
 * @author: X_Meteor
 * @description: 串口发送数据封装工具类
 * @version: V_1.0.0
 * @date: 2018/3/17 16:40
 * @company:
 * @email: lx802315@163.com
 */
public class XorUtil {
    public static String xor(String strHex_X, String strHex_Y) {
        int i;
        String anotherBinary = Integer.toBinaryString(Integer.valueOf(strHex_X, 16).intValue());
        String thisBinary = Integer.toBinaryString(Integer.valueOf(strHex_Y, 16).intValue());
        String result = "";
        if (anotherBinary.length() != 8) {
            for (i = anotherBinary.length(); i < 8; i++) {
                anotherBinary = "0" + anotherBinary;
            }
        }
        if (thisBinary.length() != 8) {
            for (i = thisBinary.length(); i < 8; i++) {
                thisBinary = "0" + thisBinary;
            }
        }
        for (i = 0; i < anotherBinary.length(); i++) {
            if (thisBinary.charAt(i) == anotherBinary.charAt(i)) {
                result = result + "0";
            } else {
                result = result + "1";
            }
        }
        String xorFlag = Integer.toHexString(Integer.parseInt(result, 2));
        if (xorFlag.length() < 2) {
            return "0" + xorFlag;
        }
        return xorFlag;
    }
}
